﻿import QtQuick 2.11

import "."
import "_Global"
import "_Global/Popup"
import "_Global/Button"

import "_Global/Global.js" as GlobalJS
import "GameJS.js" as GameJS


//弹出详细窗口
Popup {
    signal s_StartGame(int cID, int lID, int restart)

    //opacity: 0.3
    property int nLevelID     //临时存放点选的关卡
    property bool bCanPlay   //临时存放 是否可玩 的变量
    property bool bCanRestart   //是否 可以 复习

    //是否是购买完成后 显示提示对话框
    property bool bShowBuyingResult: false

    id: levelInfo
    //color: Qt.rgba(0.5,0.5,0.5,0.9)
    //color: "#700090FF"
    color: "#00FFFFFF"
    animationType: "fade";
    easingType: Easing.Linear
    duration: 200

    showMask: true
    maskColor: "black"
    maskOpacity: 0.3

    Image {
        id: imageBack
        source: Global._FixResourcePath_R("Media/Images/Class/ClassInfo/Back.png")
        anchors.fill: parent
    }

    Text {
        id: levelText
        y: Global.gamePos.dialogMap.nTitleY
        font.pixelSize: Global.gamePos.dialogMap.nTextMsgFontSize
        color: "white"
        font.bold: true
        //style: Text.Outline
        //styleColor: "black"
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter
        anchors.horizontalCenter: parent.horizontalCenter
    }
    Text {
        id: msgText
        x: Global.gamePos.dialogMap.rectTextMsg.x
        y: Global.gamePos.dialogMap.rectTextMsg.y
        width: Global.gamePos.dialogMap.rectTextMsg.width
        height: Global.gamePos.dialogMap.rectTextMsg.height
        font.pixelSize: Global.gamePos.dialogMap.nTextMsgFontSize
        //font.family: ""
        color: "white"
        //style: Text.Outline;
        //styleColor: "black"
        //horizontalAlignment: Text.AlignHCenter
        //verticalAlignment: Text.AlignVCenter
        //horizontalAlignment: Text.AlignLeft

        wrapMode: Text.Wrap
        //lineHeight: 1.3

    }

    ImageButton2 {
        id: buttonClose        //关闭
        x: Global.gamePos.dialogMap.rectClose.x
        y: Global.gamePos.dialogMap.rectClose.y
        width: Global.gamePos.dialogMap.rectClose.width
        height: Global.gamePos.dialogMap.rectClose.height

        soundEffectClick: Global.resource.effects.effectButton
        bEffectOn: Global.config.bEffectOn

        source: Global._FixResourcePath_R("Media/Images/Class/ClassInfo/Close.png")

        onButtonClicked: {
            waitingBox.close();
            levelInfo.close();
        }
    }


    ImageButton2 {
        property int nStudyGrade: 0    //0:开始学习,1:继续学习,2:复习
        property int nStep: 0

        id: buttonStartStudy        //学习
        x: Global.gamePos.mapClassInfo.rectStudy.x
        y: Global.gamePos.mapClassInfo.rectStudy.y
        width: Global.gamePos.mapClassInfo.rectStudy.width
        height: Global.gamePos.mapClassInfo.rectStudy.height

        soundEffectClick: Global.resource.effects.effectButton
        bEffectOn: Global.config.bEffectOn

        visible: levelInfo.bCanPlay

        /*
        source: {
            switch(nStudyGrade) {
            case 0:
                return Global._FixResourcePath_R("Media/Images/Class/ClassInfo/BeginStudy.png")
            case 1:
                return Global._FixResourcePath_R("Media/Images/Class/ClassInfo/ContinueStudy.png")
            case 2:
                if(UserInfo.canReview)
                    return Global._FixResourcePath_R("Media/Images/Class/ClassInfo/FuXi.png")
                else
                    return Global._FixResourcePath_R("Media/Images/Class/ClassInfo/BuyFuXiKa.png")
            default:
                return Global._FixResourcePath_R("Media/Images/Class/ClassInfo/Back.png")
            }
        }*/



        onButtonClicked: {
            if(levelInfo.bCanPlay) {
                if(nStudyGrade == 0) {
                    s_StartGame(_private.nClassID,levelInfo.nLevelID,1)
                    levelInfo.close()
                }
                else if(nStudyGrade == 1) {
                    s_StartGame(_private.nClassID,levelInfo.nLevelID,0)
                    levelInfo.close()
                }
                else if(nStudyGrade == 2)
                    if(GameManager.userInfo.canReview) {
                        s_StartGame(_private.nClassID,levelInfo.nLevelID,0)
                        levelInfo.close()
                    }
                    else {
                        if(nStep == 0) {
                            GameManager.userInfo.canReview = false;
                            source = Global._FixResourcePath_R("Media/Images/Class/ClassInfo/ContinueBuying.png");
                            msgText.text = "复习卡可以直接复习错误单词而不必重新开始学习，详情请见官网。";
                            nStep = 1;
                        }
                        else if(nStep == 1)
                        {
                            //source = Global._FixResourcePath_R("Media/Images/Class/ClassInfo/BuyStudyCard.png");
                            Qt.openUrlExternally(Global.config.urlBuyCard + "");
                            waitingBox.show();
                        }
                    }

                _private.nCurrentLevelID = levelInfo.nLevelID
            }
        }
    }

    ImageButton2 {
        id: buttonRestartStudy        //重新开始
        x: Global.gamePos.mapClassInfo.rectRestart.x
        y: Global.gamePos.mapClassInfo.rectRestart.y
        width: Global.gamePos.mapClassInfo.rectRestart.width
        height: Global.gamePos.mapClassInfo.rectRestart.height

        soundEffectClick: Global.resource.effects.effectButton
        bEffectOn: Global.config.bEffectOn

        visible: levelInfo.bCanRestart

        source: Global._FixResourcePath_R("Media/Images/Class/ClassInfo/Restart.png")

        onButtonClicked: {
            if(levelInfo.bCanRestart) {
                levelInfo.close()
                s_StartGame(_private.nClassID,levelInfo.nLevelID,1)
                _private.nCurrentLevelID = levelInfo.nLevelID
            }
        }
    }

    ImageButton2 {
        property int nStep: 0        //第几步(0:购买;1:继续购买;2:打开学习卡;3:关闭学习卡)
        id: buttonStudyCard         //购买学习卡
        x: Global.gamePos.mapClassInfo.rectStudyCard.x
        y: Global.gamePos.mapClassInfo.rectStudyCard.y
        width: Global.gamePos.mapClassInfo.rectStudyCard.width
        height: Global.gamePos.mapClassInfo.rectStudyCard.height

        //source: Global._FixResourcePath_R("Media/Images/Class/ClassInfo/Close.png")

        soundEffectClick: Global.resource.effects.effectButton
        bEffectOn: Global.config.bEffectOn

        onButtonClicked: {
            if(nStep == 0) {
                GameManager.userInfo.flagCorrectAnswer = false;
                source = Global._FixResourcePath_R("Media/Images/Class/ClassInfo/ContinueBuying.png");
                msgText.text = "1、回答单词错误时会显示正确答案，详情请见官网；\r\n2、您也可以通过关闭学习卡和打开学习卡的图标按钮进行自由选择是否使用此功能；\r\n3、若充值后没有获取到学习卡，请稍候并重新登陆即可。";
                nStep = 1;
            }
            else if(nStep == 1)
            {
                //source = Global._FixResourcePath_R("Media/Images/Class/ClassInfo/BuyStudyCard.png");
                Qt.openUrlExternally(Global.config.urlBuyCard + "");
                waitingBox.show();
            }
            else if(nStep == 2)  //如果是 打开状态
            {
                GameManager.userInfo.flagCorrectAnswer = false;
                source = Global._FixResourcePath_R("Media/Images/Class/ClassInfo/UseStudyCard.png");
                nStep = 3;
            }
            else if(nStep == 3)
            {
                GameManager.userInfo.flagCorrectAnswer = true;
                source = Global._FixResourcePath_R("Media/Images/Class/ClassInfo/CloseStudyCard.png");
                nStep = 2;
            }
        }
    }


    Popup {
        signal finishedBuying();        //购买完毕信号

        property int nLevelID     //临时存放点选的关卡
        property bool bCanPlay   //临时存放 是否可玩 的变量
        property bool bCanRestart   //是否 可以 复习

        id: waitingBox
        //color: Qt.rgba(0.5,0.5,0.5,0.9)
        //color: "#700090FF"
        innerWidth: Global.gamePos.dialogMap.rectBack.width
        innerHeight: Global.gamePos.dialogMap.rectBack.height
        color: "#00FFFFFF"
        visible: false
        //animationType: "fade";
        //easingType: Easing.Linear
        duration: 200

        showMask: false

        Image {
            source: Global._FixResourcePath_R("Media/Images/Class/ClassInfo/Back.png")
            anchors.fill: parent
        }

        Text {
            id: waitingBoxText
            x: Global.gamePos.dialogMap.rectTextMsg.x
            y: Global.gamePos.dialogMap.rectTextMsg.y
            width: Global.gamePos.dialogMap.rectTextMsg.width
            height: Global.gamePos.dialogMap.rectTextMsg.height
            font.pixelSize: Global.gamePos.dialogMap.nTextMsgFontSize
            //font.family: ""
            font.bold: true
            color: "white"
            text: "请充值完毕后再关闭此窗口。若充值后没有获取到所购买卡片，请稍候并重新登陆即可。"
            //style: Text.Outline;
            //styleColor: "black"
            //horizontalAlignment: Text.AlignHCenter
            //verticalAlignment: Text.AlignVCenter
            //horizontalAlignment: Text.AlignLeft

            wrapMode: Text.Wrap
            //lineHeight: 1.3

        }

        ImageButton2 {
            id: closeWaitingBox        //关闭
            x: Global.gamePos.dialogMap.rectClose.x
            y: Global.gamePos.dialogMap.rectClose.y
            width: Global.gamePos.dialogMap.rectClose.width
            height: Global.gamePos.dialogMap.rectClose.height

            soundEffectClick: Global.resource.effects.effectButton
            bEffectOn: Global.config.bEffectOn

            source: Global._FixResourcePath_R("Media/Images/Class/ClassInfo/Close.png")

            onButtonClicked: {
                bShowBuyingResult = true;
                UserInfo.sl_qml_loadItemsInfo();
                waitingBox.close();
                levelInfo.close();
            }
        }
    }
    /*
    ColorButton {
        id: buttonStartStudy        //学习
        property int nStudyGrade: 0    //0:开始学习,1:继续学习,2:复习
        anchors.bottom: levelInfo.bottom
        anchors.bottomMargin: 10
        x: 40
        visible: levelInfo.bCanPlay
        Text {
            font.pixelSize: 24;
            //id: buttonStart
            text: {
                switch(parent.nStudyGrade) {
                case 0:
                    return qsTr("开始学习");
                case 1:
                    return qsTr("继续学习");
                case 2:
                    if(GameManager.userInfo.canReview)
                        return qsTr("开始复习");
                    else
                        return qsTr("购买复习卡");
                }
            }
            anchors.fill: parent
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }
        onButtonClicked: {
            if(levelInfo.bCanPlay) {
                levelInfo.close()
                if(nStudyGrade == 0)
                    s_StartGame(_private.nClassID,levelInfo.nLevelID,1)
                else if(nStudyGrade == 1)
                    s_StartGame(_private.nClassID,levelInfo.nLevelID,0)
                else if(nStudyGrade == 2)
                    if(GameManager.userInfo.canReview)
                        s_StartGame(_private.nClassID,levelInfo.nLevelID,0)
                    else
                        Qt.openUrlExternally(Global.config.urlBuyCard)

                _private.nCurrentLevelID = levelInfo.nLevelID
            }
        }
    }

    ColorButton {
        id: buttonRestartStudy      //复习
        anchors.bottom: levelInfo.bottom
        anchors.bottomMargin: 10
        x: 140
        visible: levelInfo.bCanRestart
        Text {
            font.pixelSize: 24;
            //id: buttonRestart
            text: qsTr("重新开始")
            anchors.fill: parent
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }
        onButtonClicked: {
            if(levelInfo.bCanRestart) {
                levelInfo.close()
                s_StartGame(_private.nClassID,levelInfo.nLevelID,1)
                _private.nCurrentLevelID = levelInfo.nLevelID
            }
        }
    }

    ColorButton {
        id: buttonClose
        anchors.bottom: levelInfo.bottom
        anchors.bottomMargin: 10
        x: 240
        Text {
            font.pixelSize: 24;
            //id: buttonClose
            text: qsTr("关闭窗口")
            anchors.fill: parent
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }
        onButtonClicked: {
            levelInfo.close()
        }
    }
    */

    //刷新 levelBox 的信息
    function refreshLevelInfo(levelInfoParam) {
        nLevelID = levelInfoParam.levelID

        //var studiedMaxID = GameManager.userInfo.studyInfo.classInfoMap[classID].levelList.length
        //第1关或上一关可玩,则这关可玩

        //设置学习卡状态
        if(GameManager.userInfo.flagCorrectAnswer) {  //如果存在道具
            if(buttonStudyCard.nStep < 2) {  //设置为 开关状态
                buttonStudyCard.nStep = 2;
                buttonStudyCard.source = Global._FixResourcePath_R("Media/Images/Class/ClassInfo/CloseStudyCard.png");
                //GameManager.userInfo.flagCorrectAnswer = true;
            }
        }
        else {
            buttonStudyCard.nStep = 0;
            buttonStudyCard.source = Global._FixResourcePath_R("Media/Images/Class/ClassInfo/BuyStudyCard.png");
            //GameManager.userInfo.flagCorrectAnswer = false;
        }



        //设置复习卡状态
        /*if(UserInfo.itemsInfo.canReview)
            GameManager.userInfo.canReview = true;
        else
            GameManager.userInfo.canReview = false;
            */



        //是否可玩
        if(levelInfoParam.hasStudy == 0)
            bCanPlay = false;
        else if(levelInfoParam.hasStudy == 1)
            bCanPlay = true;
        else {
            if(levelInfoParam.mistakeCount == 0)
                bCanPlay = false;
            else
                bCanPlay = true;
        }

        if(levelInfoParam.hasStudy == 2)   //已经学习过,则可以复习
            bCanRestart = true;
        else
            bCanRestart = false;


        levelText.text = "第" + (levelInfoParam.levelID + 1) + "关";
        //console.debug("hasStudy:",levelInfoParam.hasStudy);
        //console.debug("wordRecords:",levelInfoParam.wordRecords);
        if(levelInfoParam.hasStudy == 0) {  //没学
            //else {
                msgText.text = "您需要完成上一关。";
            //}
        }
        else if(levelInfoParam.hasStudy == 1) { //没学完(学习)(包括最新一关)
            if(levelInfoParam.mistakeCount == 0 && levelInfoParam.correctCount == 0) {
                //if(levelInfoParam.levelID == 0) {
                    //msgText.text = "此关有%1个单词，请点击开始学习".arg(levelInfoParam.wordCount);
                    msgText.text = "每关有20个单词，请点击开始学习。";
                    buttonStartStudy.nStudyGrade = 0;
                //}
            }
            else {

                //msgText.text = "总数:" + levelInfoParam.wordCount + "";
                //msgText.text += "未学习:" + (levelInfoParam.wordCount - levelInfoParam.mistakeCount - levelInfoParam.correctCount)
                msgText.text = "本关已经学习%1个单词，还有%2个单词未学，是否继续学习？".arg(levelInfoParam.mistakeCount + levelInfoParam.correctCount).arg(levelInfoParam.wordCount - levelInfoParam.mistakeCount - levelInfoParam.correctCount)
                buttonStartStudy.nStudyGrade = 1;
            }
        }
        else {      //已学完(复习)
            /*
            msgText.text = "总数:" + levelInfoParam.wordCount;
            msgText.text += "正确单词:" + (levelInfoParam.correctCount)
            msgText.text += "错误单词:" + (levelInfoParam.mistakeCount)*/
            msgText.text = "本关单词全部学完，目前有%1个错误单词。".arg(levelInfoParam.mistakeCount)
            if(levelInfoParam.mistakeCount != 0) {
                if(GameManager.userInfo.canReview)
                    msgText.text += "是否复习？"
                else
                    msgText.text += "您可以购买复习卡进行复习，也可以重新开始（重新开始错误单词将清零重新计算）。"
            }

            if(levelInfoParam.mistakeCount > 0)
                buttonStartStudy.nStudyGrade = 2;
        }




        //设置 第一个 按钮
        buttonStartStudy.nStep = 0;
        switch(buttonStartStudy.nStudyGrade) {
        case 0:
            buttonStartStudy.source = Global._FixResourcePath_R("Media/Images/Class/ClassInfo/BeginStudy.png")
            break;
        case 1:
            buttonStartStudy.source = Global._FixResourcePath_R("Media/Images/Class/ClassInfo/ContinueStudy.png")
            break;
        case 2:
            if(UserInfo.canReview)
                buttonStartStudy.source = Global._FixResourcePath_R("Media/Images/Class/ClassInfo/FuXi.png")
            else
                buttonStartStudy.source = Global._FixResourcePath_R("Media/Images/Class/ClassInfo/BuyFuXiKa.png")
            break;
        default:
            console.error("buttonStartStudy.nStudyGrade:", buttonStartStudy.nStudyGrade)
        }

        //console.debug(levelInfoParam.wordRecords)
        //msgText.text += levelInfoParam.hasStudy
        //msgText.text += levelInfo.bCanPlay
    }

}
